﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using YouHP.IDAL;
using YouHP.Model;

namespace YouHP.SqlServerDAL
{
	public class UserActivityDAL : IUserActivity
	{
		#region basic operations
        public bool AddUserActivity(UserActivityInfo info)
		{
			return SqlHelper.ExecuteNonQuery(Common.ConnStr,
				Common.UserActivitySP_AddUserActivity,
				info.ActivityId,
				info.UserId,
				info.UserName,
				info.Message,
				info.Connactinfo,
				info.IsVerify,
				info.ApplyDate,
				info.Reply
				) > 0;
		}
		
		public bool UpdateUserActivity(UserActivityInfo info)
		{
			return SqlHelper.ExecuteNonQuery(Common.ConnStr,
				Common.UserActivitySP_UpdateUserActivity,
				info.Id,
				info.ActivityId,
				info.UserId,
				info.UserName,
				info.Message,
				info.Connactinfo,
				info.IsVerify,
				info.ApplyDate
				) > 0;
		}

        public bool CheckUserActivity(UserActivityInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                Common.UserActivitySP_CheckUserActivity,
                info.Id,
                info.IsVerify,
                info.Reply,
                info.ReplyDate
                ) > 0;
        }
		
		public bool DeleteUserActivity(int[] ids)
		{
			int cnt = ids.Length;
			if (cnt <= 0)
				return false;
			
			StringBuilder sb = new StringBuilder();
			sb.Append("[id] in (");
			sb.Append(ids[0]);
			
			for (int i = 1; i < cnt; i++)
			{
				sb.Append(", ");
				sb.Append(ids[i]);
			}
			sb.Append(")");
			
			return SqlHelper.ExecuteNonQuery(Common.ConnStr,
				Common.UserActivitySP_DelUserActivity,
				sb.ToString()) >= cnt;		
		}
		
		public UserActivityInfo GetUserActivityInfo(int id)
		{
			IDataReader reader = SqlHelper.ExecuteReader(Common.ConnStr,
				Common.UserActivitySP_GetUserActivity,
				id) as IDataReader;
			return CBO.FillObject<UserActivityInfo>(reader);
		}
		#endregion
	
		#region list and search operations
		private List<UserActivityInfo> GetUserActivityList(int pageSize, int pageIndex, string condStr, string orderStr, out int count)
		{
			DataSet dset = SqlHelper.ExecuteDataset(Common.ConnStr,
				Common.GetFullName(Common.UserActivitySP_GetUserActivityList),
				pageSize, pageIndex, condStr, orderStr);
			
			IDataReader dr = dset.Tables[0].CreateDataReader();
			if (dr != null && dr.Read())
			{
				count = dr.GetInt32(0);
				dr.Close();
			}
			else
			{
				count = 0;
			}
			dr = dset.Tables[1].CreateDataReader();
			
			return CBO.FillCollection<UserActivityInfo>(dr);
		}
		
		public List<UserActivityInfo> GetUserActivityList(int pageSize, int pageIndex, out int count)
		{
			return GetUserActivityList(pageSize, pageIndex, "", " [UserActivity].[Id] desc ", out count);
		}

        public List<UserActivityInfo> GetUserActivityByUserName(int pageSize, int pageIndex, string keyword, out int count)
        {
            string condStr = string.Format(@" [UserName] like '%{0}%' ", keyword);
            return GetUserActivityList(pageSize, pageIndex, condStr, " [UserActivity].[Id] desc ", out count);
        }

        public List<UserActivityInfo> GetUserActivityByActivityId(int pageSize, int pageIndex, int activityId, string keyword, out int count)
        {
            string condStr = string.Format(@" [ActivityId]={0} and [UserName] like '%{1}%' ", activityId, keyword);
            return GetUserActivityList(pageSize, pageIndex, condStr, " [UserActivity].[Id] desc ", out count);
        }
        #endregion
    }
}

